package com.mumu.muPictureBackend.manager;

import cn.hutool.core.io.FileUtil;
import com.mumu.muPictureBackend.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Component
public class CosManager {
    @Resource
    private CosClientConfig cosClientConfig;
    @Resource
    private COSClient cosClient;

    /**
     * 将文件上传到COS中(上传通用图片)
     *
     * @param key  指定用哪个key
     * @param file 要上传的文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        //指定桶 key 文件
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        //上传到cos上
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 从cos中将文件下载下来本地
     *
     * @param key
     * @return
     */
    public COSObject getObject(String key) {
        //指定桶 key 文件
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        //上传到cos上
        return cosClient.getObject(getObjectRequest);
    }

    public void deleteObject(String key) {
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(cosClientConfig.getBucket(), key);
        cosClient.deleteObject(deleteObjectRequest);
    }


    /**
     * 上传图片(需要开通腾讯云的数据万象)
     *
     * @param key
     * @param file
     * @return
     */
    public PutObjectResult putPictureObject(String key, File file) {
        PutObjectRequest objectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        //picOperations是COS提供的一个类 用于配置对图片的操作
        PicOperations picOperations = new PicOperations();
        picOperations.setIsPicInfo(1);//表示保持原图的信息
        //图片处理规则列表 这是一个集合 因为需要添加多个规则 拿到一个图片后需要分别去设置这些规则从而返回对应的图片
        List<PicOperations.Rule> rules = new ArrayList<>();
        //将图片转换成webp格式(获取该文件的原始文件名然后拼上.webp) 例如将"photos/abc.png" → "photos/abc"
        String webpKey = FileUtil.mainName(key) + ".webp";
        //新建一个压缩成webp的规则
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setFileId(webpKey);
        //对哪个桶中的文件进行处理
        compressRule.setBucket(cosClientConfig.getBucket());
        compressRule.setRule("imageMogr2/format/webp");//指定转换方式 将该文件转换成webp格式 该格式的图片兼容性很好 并且比PNG JPEG格式都小25%左右
        rules.add(compressRule);
        //只有图片大于20kb才设置缩略图 否则适得其反
        if (file.length() >= 20 * 1024) {
            //新建一个压缩缩略图的规则
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            //拼接缩略图的路径
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnail." + cn.hutool.core.io.FileUtil.getSuffix(key);
            thumbnailRule.setFileId(thumbnailKey);
            thumbnailRule.setBucket(cosClientConfig.getBucket());
            //set缩略 set缩略规则: /thumbnail/ Width x Height> (如果这个宽高大于了原图(原图更小) 那么就不处理)
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>", 1024, 1024));
            //添加缩略规则
            rules.add(thumbnailRule);
        }
        //构造处理参数
        picOperations.setRules(rules);
        objectRequest.setPicOperations(picOperations);//将picOperations添加到上传请求中
        return cosClient.putObject(objectRequest);//将该图片上传到cos上
    }


}
